package com.yunyike.controller.summary;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yunyike.constants.CommonConstant;
import com.yunyike.constants.RequestConstant;
import com.yunyike.constants.ResponseConstant;
import com.yunyike.controller.BaseController;
import com.yunyike.dto.SummaryMerchandiseDTO;
import com.yunyike.model.SkuSummary;
import com.yunyike.service.BrandService;
import com.yunyike.service.MerchandiseService;
import com.yunyike.service.PriceService;
import com.yunyike.service.SkuService;
import com.yunyike.service.SkuSummaryService;

@Controller
@RequestMapping(value=RequestConstant.SUMMARYMERCHANDISE)
public class SummaryMerchandiseController extends BaseController{

	private Logger log = LoggerFactory.getLogger(SummaryMerchandiseController.class);
	
	@Autowired
	private MerchandiseService merchandiseService;
	@Autowired
	private SkuService skuService;
	@Autowired
	private PriceService priceService;
	@Autowired
	private BrandService brandService;

	@Autowired
	private SkuSummaryService skuSummaryService;
	
	@RequestMapping(value=RequestConstant.SUMMARYLIST)
	public String toMerchandiseList(HttpServletRequest request, HttpServletResponse response) {
		log.info("SUMMARYMERCHANDISE toMerchandiseList--");
		
		try {
			List<Integer> skustatus = new ArrayList<Integer>();
			skustatus.add(CommonConstant.SKU_STOCKPILING);//已囤货
			skustatus.add(CommonConstant.SKU_UNTREATED);//未囤货--》已发布
			List<SummaryMerchandiseDTO> summaryList = merchandiseService.selectSummaryMerchandise(skustatus);
			skuSummaryService.checkSummaryDto(summaryList);
			request.setAttribute("summaryList", summaryList);
			request.setAttribute("SkuStatusMap", CommonConstant.getSkuStatusMap());
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("message", "发生异常");
		}
		log.info("SUMMARYMERCHANDISE toMerchandiseList--END--");
		return ResponseConstant.SUMMARYLIST;
	}
	@ResponseBody
	@RequestMapping(value=RequestConstant.SAVESUMMARYLIST)
	public String saveSummary(HttpServletRequest request, HttpServletResponse response) {
		log.info("SUMMARYMERCHANDISE saveSummary--");
		
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String skuId = request.getParameter("skuId");
			String skuSummaryQty = request.getParameter("skuSummaryQty");
			String skuSummaryPrice = request.getParameter("skuSummaryPrice");
			String skuSummaryOrderTime = request.getParameter("skuSummaryOrderTime");
			String skuSummaryRukuTime = request.getParameter("skuSummaryRukuTime");
			String skuSummaryRukuChecker = request.getParameter("skuSummaryRukuChecker");
			String skuSummaryRukuComment = request.getParameter("skuSummaryRukuComment");
			String skuSummarySellTime = request.getParameter("skuSummarySellTime");
			String skuSummarySellPrice = request.getParameter("skuSummarySellPrice");
			SkuSummary summary = skuSummaryService.selectBySkuId(skuId);
			int num =0;
			if(summary==null){
				summary = new SkuSummary();
				summary.setSkuId(skuId);
				summary.setSkuSummaryId(UUID.randomUUID().toString());
				summary.setSkuSummaryOrderTime("".equals(skuSummaryOrderTime)?null:sdf.parse(skuSummaryOrderTime));
				summary.setSkuSummaryPrice("".equals(skuSummaryPrice)?null:Double.parseDouble(skuSummaryPrice));
				summary.setSkuSummaryQty("".equals(skuSummaryQty)?null:Integer.parseInt(skuSummaryQty));
				summary.setSkuSummaryRukuChecker(skuSummaryRukuChecker);
				summary.setSkuSummaryRukuComment(skuSummaryRukuComment);
				summary.setSkuSummaryRukuTime("".equals(skuSummaryRukuTime)?null:sdf.parse(skuSummaryRukuTime));
				summary.setSkuSummarySellPrice("".equals(skuSummarySellPrice)?null:Double.parseDouble(skuSummarySellPrice));
				summary.setSkuSummarySellTime("".equals(skuSummarySellTime)?null:sdf.parse(skuSummarySellTime));
				num = skuSummaryService.insert(summary);
			}else{
				summary.setSkuSummaryOrderTime("".equals(skuSummaryOrderTime)?null:sdf.parse(skuSummaryOrderTime));
				summary.setSkuSummaryPrice("".equals(skuSummaryPrice)?null:Double.parseDouble(skuSummaryPrice));
				summary.setSkuSummaryQty("".equals(skuSummaryQty)?null:Integer.parseInt(skuSummaryQty));
				summary.setSkuSummaryRukuChecker(skuSummaryRukuChecker);
				summary.setSkuSummaryRukuComment(skuSummaryRukuComment);
				summary.setSkuSummaryRukuTime("".equals(skuSummaryRukuTime)?null:sdf.parse(skuSummaryRukuTime));
				summary.setSkuSummarySellPrice("".equals(skuSummarySellPrice)?null:Double.parseDouble(skuSummarySellPrice));
				summary.setSkuSummarySellTime("".equals(skuSummarySellTime)?null:sdf.parse(skuSummarySellTime));
				num = skuSummaryService.updateByPrimaryKey(summary);
			}
			if(num<1){
				return "error";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "发生异常";
		}
		log.info("SUMMARYMERCHANDISE saveSummary--END--");
		return "success";
	}
}
